package com.bao.demo.work.flower.controller;


import com.bao.demo.utils.BaseDAO;
import com.bao.demo.work.flower.entity.Flower;
import com.bao.demo.work.flower.service.FlowerService;
import com.bao.demo.work.flower.service.impl.FlowerServiceImpl;

import java.sql.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author baoye
 * @date 2024-08-23/19:15
 */
public class FlowerController {
    public static void select(){
        String sql="select * from flower";
        List<Flower> flowers = null;
        try {
            flowers = FlowerServiceImpl.class.newInstance().selectAllFlower(sql);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        String header="%-10s%-15s%-10s%-10s%-15s%-10s%-10s";
        String format = String.format(header,"编号","鲜花名称","销售数量","价格","销售日期","销售员","备注");
        System.out.println(format);

        assert flowers != null;
        for (Flower flower : flowers) {
            printFlower(flower);
        }
    }

    public static void update(){
        FlowerServiceImpl flowerService = null;
        try {
            flowerService = FlowerServiceImpl.class.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        String sql="select * from flower where id=?";
        Scanner input = new Scanner(System.in);

        System.out.println("请输入想要修改的编号");
        int id=input.nextInt();
        assert flowerService != null;
        Flower flower = flowerService.selectFlowerById(sql,id);
        if(flower==null) {
            System.out.println("\u001B[31m编号不存在\u001B[0m");
            return;
        }
        String header="%-10s%-15s%-10s%-10s%-15s%-10s%-10s";
        String format = String.format(header,"编号","鲜花名称","销售数量","价格","销售日期","销售员","备注");
        System.out.println(format);

        printFlower(flower);

        System.out.println("请输入修改后的商品名称");
        String name=input.next();
        System.out.println("请输入修改后的商品数量");
        int num=input.nextInt();
        System.out.println("请输入修改后的商品价格");
        double price=input.nextDouble();
        System.out.println("请输入修改后的商品销售日期(yyyy-MM-dd)");
        String date=input.next();
        System.out.println("请输入修改后的商品销售员");
        String emp=input.next();
        System.out.println("请输入修改后的备注");
        String comment=input.next();

        String sql2="update flower set name=?,num=?,price=?,buyDate=?,emp=?,comment=? where id=?";

        Flower newFlower = new Flower(id, name, num, price, Date.valueOf(date), emp, comment);

        int updateRow = BaseDAO.update(sql2, name, num, price, date, emp, comment, id);

        if(updateRow==1){
            System.out.println("修改成功");
            printFlower(newFlower);
        }else {
            System.out.println("修改失败");
        }


    }

    public static void delete(){
        FlowerServiceImpl flowerService = null;
        try {
            flowerService = FlowerServiceImpl.class.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        String sql="select * from flower where id=?";
        Scanner input = new Scanner(System.in);

        System.out.println("请输入想要删除的编号");
        int id=input.nextInt();
        assert flowerService != null;
        Flower flower = flowerService.selectFlowerById(sql,id);
        if(flower==null) {
            System.out.println("\u001B[31m编号不存在\u001B[0m");
            return;
        }
        String header="%-10s%-15s%-10s%-10s%-15s%-10s%-10s";
        String format = String.format(header,"编号","鲜花名称","销售数量","价格","销售日期","销售员","备注");
        System.out.println(format);

        printFlower(flower);

        String sql2 = "delete from flower where id=?";

        System.out.println("是否删除(Y/N)");
        if("Y".equals(input.next())){
            int deleteRow = BaseDAO.deleteOne(sql2, id);
            if(deleteRow==1){
                System.out.println("删除成功");
            }
            else {
                System.out.println("删除失败");
            }
        }else {
            System.out.println("取消删除");
        }

    }
    private static void printFlower(Flower flower){
        String[] split = flower.toString().split(",");
        if("null".equals(split[split.length-1])){
            split[split.length-1]="";
        }
        String values="%-11s%-14s%-11s%-10s%-19s%-10s%-10s";
        String s = String.format(values, (Object[]) split);
        System.out.println(s);
    }
}
